package com.wteam.dragon.model.controller;

import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wteam.dragon.model.pojo.bean.User;
import com.wteam.dragon.model.pojo.dto.UserDto;
import com.wteam.dragon.model.pojo.vo.QueryUserVo;
import com.wteam.dragon.model.pojo.vo.UserVo;
import com.wteam.dragon.model.service.UserService;
import com.wteam.dragon.system.aop.log.log.Log;
import com.wteam.dragon.system.basebean.bean.ResultMessage;
import com.wteam.dragon.system.basebean.vo.PageVo;
import com.wteam.dragon.system.util.BeanUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Set;

/**
 * @Author 陈晓辉
 * @Date 2020/12/18 10:57
 */
@RestController
@RequestMapping("admin/user")
@Api(tags = "系统：用户管理")
@RequiredArgsConstructor
public class UserController {
    private final UserService userService;

    @Value("${rsa.private_key}")
    private String privateKey;

    @Log("获得分页的用户列表")
    @GetMapping("pageUser")
    @ApiOperation("获得分页的用户列表")
    @PreAuthorize("@el.check('pageUser')")
    public ResultMessage pageUser(PageVo<UserDto> pageVo, QueryUserVo queryUserVo){
        Page<UserDto> page = BeanUtil.fromPage(pageVo);
        return userService.pageUser(page, queryUserVo);
    }

    @Log("添加用户")
    @PostMapping("addUser")
    @ApiOperation("添加用户")
    @PreAuthorize("@el.check('addUser')")
    public ResultMessage addUser(UserVo userVo, @RequestParam Set<Long> rolesId, @RequestParam(required = false) Set<Long> jobsId){
        User user = BeanUtil.from(userVo, User.class);
        return userService.addUser(user, rolesId, jobsId);
    }

    @Log("更改密码")
    @PutMapping("editPass")
    @ApiOperation("更改密码")
    @PreAuthorize("@el.check('editPass')")
    public ResultMessage editPass(String oldPass, String newPass){
        // 密码解密
        RSA rsa = new RSA(privateKey, null);
        newPass = new String(rsa.decrypt(newPass, KeyType.PrivateKey));
        oldPass = new String(rsa.decrypt(oldPass, KeyType.PrivateKey));
        return userService.editPass(oldPass, newPass);
    }

    @Log("获得当前用户的级别")
    @GetMapping("getUserLevel")
    @ApiOperation("获得当前用户的级别")
    @PreAuthorize("@el.check('getUserLevel')")
    public ResultMessage getUserLevel(){
        return userService.getUserLevel();
    }

    @Log("更改用户信息")
    @PutMapping("editUser")
    @ApiOperation("更改用户信息")
    @PreAuthorize("@el.check('editUser')")
    public ResultMessage editUser(UserVo userVo, @RequestParam Set<Long> rolesId, @RequestParam(required = false) Set<Long> jobsId){
        User user = BeanUtil.from(userVo, User.class);
        return userService.editUser(user, rolesId, jobsId);
    }

    @Log("编辑头像")
    @PostMapping("editAvatar")
    @ApiOperation("编辑头像")
    @PreAuthorize("@el.check('editAvatar')")
    public ResultMessage editAvatar(MultipartFile avatar){
        return userService.editAvatar(avatar);
    }

    @Log("删除用户")
    @DeleteMapping("delUser")
    @ApiOperation("删除用户")
    @PreAuthorize("@el.check('delUser')")
    public ResultMessage delUser(@RequestParam("ids") Set<Long> ids){
        return userService.delUser(ids);
    }

    @Log("更改自己账号的信息")
    @PutMapping("editMyUser")
    @ApiOperation("更改自己账号的信息")
    @PreAuthorize("@el.check('editMyUser')")
    public ResultMessage editMyUser(UserVo userVo){
        User user = BeanUtil.from(userVo, User.class);
        return userService.editMyUser(user);
    }
}
